package com.dayou.service.impl;

import java.util.List;

import com.dayou.common.exception.BusinessException;
import com.dayou.common.utils.StringUtils;
import com.dayou.mapper.SysUserMapper;
import com.dayou.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dayou.mapper.SysDeptMapper;
import com.dayou.bean.SysDept;
import com.dayou.service.ISysDeptService;
import com.dayou.common.core.text.Convert;
import com.dayou.bean.SysUser;

/**
 * 达幽系统集成框架 - 部门 服务层实现
 * 
 * @author dayou
 * @date 2020-04-24
 */
@Service
public class SysDeptService implements ISysDeptService
{
	@Autowired
	private SysDeptMapper sysDeptMapper;
	@Autowired
	private SysUserMapper sysUserMapper;

	/**
     * 查询达幽系统集成框架 - 部门信息
     * 
     * @param id 达幽系统集成框架 - 部门ID
     * @return 达幽系统集成框架 - 部门信息
     */
    @Override
	public SysDept selectSysDeptById(Integer id)
	{
	    return sysDeptMapper.selectSysDeptById(id);
	}
	
	/**
     * 查询达幽系统集成框架 - 部门列表
     * 
     * @param sysDept 达幽系统集成框架 - 部门信息
     * @return 达幽系统集成框架 - 部门集合
     */
	@Override
	public List<SysDept> selectSysDeptList(SysDept sysDept)
	{
	    return sysDeptMapper.selectSysDeptList(sysDept);
	}
	
    /**
     * 新增达幽系统集成框架 - 部门
     * 
     * @param sysDept 达幽系统集成框架 - 部门信息
     * @return 结果
     */
	@Override
	public int insertSysDept(SysDept sysDept)
	{
		sysDept.setCreateBy(UserUtil.getUser().getId());
	    return sysDeptMapper.insertSysDept(sysDept);
	}
	
	/**
     * 修改达幽系统集成框架 - 部门
     * 
     * @param sysDept 达幽系统集成框架 - 部门信息
     * @return 结果
     */
	@Override
	public int updateSysDept(SysDept sysDept)
	{
		sysDept.setUpdateBy(UserUtil.getUser().getId());
	    return sysDeptMapper.updateSysDept(sysDept);
	}

	/**
	 * 伪删除达幽系统集成框架 - 部门
	 *
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
    @Override
    public int fakeDeleteSysDeptByIds(String ids)
	{
        Long userId = UserUtil.getUser().getId();
        if(StringUtils.isEmpty(ids)){
            throw new BusinessException("未获取到id");
        }else{
            String[] idsArr = Convert.toStrArray(ids);
            if(idsArr.length == 1){
				Integer id = null;
                try{
                    id = Integer.parseInt(idsArr[0]);
                }catch (Exception ex){
                    throw new BusinessException("id有误");
                }
				List<SysUser> users = sysUserMapper.selectSysUserByDeptId(id);
				if(users.size() > 0){
					throw new BusinessException("该部门已分配用户，请先删除该部门下的用户");
				}
				return sysDeptMapper.fakeDeleteSysDeptById(id,userId);
            }else{
				List<SysUser> users = sysUserMapper.selectSysUserByDeptIds(idsArr);
				if(users.size() > 0){
					throw new BusinessException("所选部门包含已分配用户的部门，请先删除该部门下的用户");
				}
                return sysDeptMapper.fakeDeleteSysDeptByIds(idsArr,userId);
            }
        }
    }

	/**
	 * 恢复达幽系统集成框架 - 部门
	 *
	 * @param ids 需要恢复的数据ID
	 * @return 结果
	 */
    public int resumeSysDeptByIds(String ids)
	{
        if(StringUtils.isEmpty(ids)){
            throw new BusinessException("未获取到id");
        }else{
            String[] idsArr = Convert.toStrArray(ids);
            if(idsArr.length == 1){
				Integer id = null;
                try{
                    id = Integer.parseInt(idsArr[0]);
                }catch (Exception ex){
                    throw new BusinessException("id有误");
                }
				return sysDeptMapper.resumeSysDeptById(id);
            }else{
                return sysDeptMapper.resumeSysDeptByIds(idsArr);
            }
        }
    }
	/**
     * 删除达幽系统集成框架 - 部门对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteSysDeptByIds(String ids)
	{
	    if(StringUtils.isEmpty(ids)){
	        throw new BusinessException("未获取到id");
		}else{
	        String[] idsArr = Convert.toStrArray(ids);
	        if(idsArr.length == 1){
                try{
                    Integer id = Integer.parseInt(idsArr[0]);
                    return sysDeptMapper.deleteSysDeptById(id);
				}catch (Exception ex){
	                throw new BusinessException("id有误");
				}
			}else{
                return sysDeptMapper.deleteSysDeptByIds(idsArr);
            }
		}
	}
}
